/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.portlet.editor.gui.view.settings.table;

import java.awt.Component;

import javax.swing.AbstractCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

import com.genia.toolbox.basics.editor.model.document.VariableModel;
import com.genia.toolbox.portlet.editor.gui.PortletEditorGUI;

/**
 * The variable table cell editor. Used for attributes and parameters.
 */
@SuppressWarnings("serial")
public class VariableCellEditor
    extends AbstractCellEditor
    implements TableCellEditor
{

  /**
   * The application portletEditorGUI.
   */
  private PortletEditorGUI portletEditorGUI;

  /**
   * The current component.
   */
  private Component component = null;



  /**
   * Constructor.
   * 
   * @param portletEditorGUI
   *          the application portletEditorGUI.
   */
  public VariableCellEditor(PortletEditorGUI portletEditorGUI)
  {
    super();
    this.portletEditorGUI = portletEditorGUI;
  }



  /**
   * Handle the display.
   * 
   * @param table
   *          The table.
   * @param value
   *          The displayed value.
   * @param isSelected
   *          Whether the cell is selected.
   * @param row
   *          The row index.
   * @param column
   *          The column index.
   * @return the component.
   */
  public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
  {
    VariableTableModel tableModel = (VariableTableModel) table.getModel();
    VariableModel variable = tableModel.getValueAt(row);

    if (column == 0) {
      JLabel label = new JLabel();
      label.setText(" " + this.portletEditorGUI.getController().notifyTranslation(variable.getName()));
      this.component = label;
    }
    else {
      if (variable.hasValues()) {
        DefaultComboBoxModel comboModel = new DefaultComboBoxModel();
        for (String possibleValue : variable.getValues().values()) {
          comboModel.addElement(possibleValue);
        }
        JComboBox comboBox = new JComboBox(comboModel);
        comboBox.setEditable(false);
        if (variable.getValue() != null) {
          comboBox.setSelectedItem(variable.getValue());
        }
        this.component = comboBox;
      }
      else if (variable.isLongValue()) {
        JTextField textField = new JTextField();
        PlainDocument document = new PlainDocument()
        {
          @Override
          public void insertString(int offs, String str, AttributeSet a)
              throws BadLocationException
          {
            StringBuilder buffer = new StringBuilder();
            if (str != null && str.trim().length() != 0) {
              char[] chars = str.toCharArray();
              for (char element : chars) {
                if (Character.isDigit(element)) {
                  buffer.append(element);
                }
              }
            }
            super.insertString(offs, buffer.toString(), a);
          }
        };
        textField.setDocument(document);
        textField.setEditable(true);
        textField.setText(variable.getValue());
        this.component = textField;
      }
      else {
        JTextField textField = new JTextField();
        textField.setEditable(true);
        textField.setText(variable.getValue());
        this.component = textField;
      }
    }

    return this.component;
  }



  /**
   * Get the cell value.
   * 
   * @return the cell value.
   */
  public Object getCellEditorValue()
  {
    Object value = null;
    if (this.component != null) {
      if (this.component instanceof JLabel) {
        value = ((JLabel) this.component).getText();
      }
      else if (this.component instanceof JComboBox) {
        value = ((JComboBox) this.component).getSelectedItem();
      }
      else if (this.component instanceof JTextField) {
        value = ((JTextField) this.component).getText();
      }
    }
    return value;
  }

}
